package org.jeecg.modules.power.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.app.v1.util.Const;
import org.jeecg.modules.power.device.entity.YlDeviceOperateRecords;
import org.jeecg.modules.power.device.mapper.YlDeviceOperateRecordsMapper;
import org.jeecg.modules.power.device.service.IYlDeviceOperateRecordsService;
import org.jeecg.modules.power.group.entity.GroupUser;
import org.jeecg.modules.system.entity.SysUser;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.Date;
import java.util.List;

/**
 * @Description: yl_device_operate_records
 * @Author: jeecg-boot
 * @Date:   2024-08-09
 * @Version: V1.0
 */
@Service
public class YlDeviceOperateRecordsServiceImpl extends ServiceImpl<YlDeviceOperateRecordsMapper, YlDeviceOperateRecords> implements IYlDeviceOperateRecordsService {


    /**
     * 仓宝运维记录
     * @param deviceId
     * @param beginTime ，endTime
     * @param type
     * @return
     */
    public List<YlDeviceOperateRecords> getRecords(Page<YlDeviceOperateRecords> page, String deviceId, String beginTime, String endTime, String type){
        String bTime = oConvertUtils.isNotEmpty(beginTime)?String.format("%sT%s",beginTime, Const.TIME_000000):"";
        String eTime = oConvertUtils.isNotEmpty(endTime)?String.format("%sT%s",endTime,Const.TIME_235959):"";
        QueryWrapper<YlDeviceOperateRecords> queryWrapper = new QueryWrapper<>();
        queryWrapper
                .and(e->e.eq("device_id",deviceId).or().eq("device_mac",deviceId))
                .ge(oConvertUtils.isNotEmpty(bTime),"create_time",bTime)
                .le(oConvertUtils.isNotEmpty(eTime),"create_time",eTime)
                .eq(oConvertUtils.isNotEmpty(type),"operate_type",type);
        return this.page(page,queryWrapper).getRecords();
    }

    /**
     * 仓宝运维
     * 此处有针对设备的操作，后续补充
     * @param deviceId 设备ID或者mac
     * @param type 运维类型（字典 device_operate_type） ：type:1强制弹宝2正常弹宝3锁仓4解锁
     * @param hatch 充电宝信息{仓口号,SN,执行状态}
     */
    public void operate(String deviceId, String deviceMac, Integer type, String hatch,String reason, GroupUser bd){
        YlDeviceOperateRecords records = new YlDeviceOperateRecords();
        records.setDeviceId(deviceId);
        records.setDeviceMac(deviceMac);
        records.setOperateType(type);
        records.setPowerBank(hatch);
        records.setOperateReason(reason);
        records.setCreateBy(bd.getUserInfo());
        records.setCreateTime(new Date());
        this.save(records);

    }
}
